package com.chenchao.thread;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/16 18:15
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javathread.Test5.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 线程等待与唤醒
 */
public class Test5 {
    public static void main(String[] args) {
        test1();
    }
    public static void test1(){
        Object o = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o){
                    System.out.println("网管，来碗泡面。");
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("结账");
                    o.notify();
                    //如果有多个，随机唤醒一个
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o){
                    System.out.println("4.5元一桶。");
                    o.notify();
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("找您0.5元");
                }

            }
        }).start();

    }
}

